Micron Document
`:top
A `!property`!, in some `F33f`_`[object-oriented`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f `F33f`_`[programming languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f, is a special sort of `F33f`_`[class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(computer_science)]`_`f member, intermediate in functionality between a `F33f`_`[field`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Field_(computer_science)]`_`f (or data member) and a `F33f`_`[method`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Method_(computer_science)]`_`f. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to '`F33f`_`[getter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Accessor_method]`_`f' and '`F33f`_`[setter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutator_method]`_`f' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for `F33f`_`[data validation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_validation]`_`f, `F33f`_`[active updating`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Active_updating]`_`f (e.g., of `F33f`_`[GUI`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GUI]`_`f elements), or implementation of what may be called "read-only fields".

>>Contents

• `F0af`_`[Support in languages`#support-in-languages]`_`f
• `F0af`_`[Syntax variants`#syntax-variants]`_`f
• `F0af`_`[Dot notation`#dot-notation]`_`f
• `F0af`_`[Bracket notation`#bracket-notation]`_`f
• `F0af`_`[Example syntax`#example-syntax]`_`f
• `F0af`_`[C#`#c]`_`f
• `F0af`_`[C++`#c]`_`f
• `F0af`_`[D`#d]`_`f
• `F0af`_`[Delphi/Free Pascal`#delphi-free-pascal]`_`f
• `F0af`_`[eC`#ec]`_`f
• `F0af`_`[F#`#f]`_`f
• `F0af`_`[JavaScript`#javascript]`_`f
• `F0af`_`[ActionScript 3.0`#actionscript-3-0]`_`f
• `F0af`_`[Objective-C 2.0`#objective-c-2-0]`_`f
• `F0af`_`[PHP`#php]`_`f
• `F0af`_`[Python`#python]`_`f
• `F0af`_`[Ruby`#ruby]`_`f
• `F0af`_`[Visual Basic`#visual-basic]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>Support in languages

Programming languages that support properties include `F33f`_`[ActionScript 3`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ActionScript_3]`_`f, `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f, `F33f`_`[D`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=D_(programming_language)]`_`f, `F33f`_`[Delphi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_Pascal]`_`f/`F33f`_`[Free Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Free_Pascal]`_`f, eC, `F33f`_`[F#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=F_Sharp_(programming_language)]`_`f, `F33f`_`[Kotlin`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Kotlin_(programming_language)]`_`f, `F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f, `F33f`_`[Objective-C 2.0`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Objective-C]`_`f, `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f, `F33f`_`[Scala`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scala_(programming_language)]`_`f, `F33f`_`[Swift`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Swift_(programming_language)]`_`f, `F33f`_`[Lua`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lua_(programming_language)]`_`f, and `F33f`_`[Visual Basic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Visual_Basic]`_`f.

Some object-oriented languages, such as `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f and `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f, do not support properties, requiring the programmer to define a pair of `*`F33f`_`[accessor`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Accessor_method]`_`f`* and `*`F33f`_`[mutator`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutator_method]`_`f`* methods instead.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]

`F33f`_`[Oberon-2`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Oberon-2]`_`f provides an alternative mechanism using object variable visibility flags.

Other languages designed for the `F33f`_`[Java Virtual Machine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_virtual_machine]`_`f, such as `F33f`_`[Groovy`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Groovy_(programming_language)]`_`f, natively support properties.

While `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f does not have first class properties, they can be emulated with `F33f`_`[operator overloading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Operator_overloading]`_`f.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]

Also note that some C++ compilers support first class properties as language extensions.

• In `F33f`_`[Microsoft Visual Studio`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_Visual_Studio]`_`f,`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f] `F33f`_`[GCC`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GNU_Compiler_Collection]`_`f, and `F33f`_`[llvm/clang`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=LLVM]`_`f,`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f] the `!`B100`F9d9__declspec(property)`f`b`! creates properties similar to `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f.
• `F33f`_`[Borland C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Borland_C++]`_`f and `F33f`_`[Borland/CodeGear/Embarcadero C++Builder`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++Builder]`_`f use the `!`B100`F9d9__property`f`b`! keyword.`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]

In many object oriented languages properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a `*read-only`* or an uncommon `*write-only`* property.

In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in `F33f`_`[Perl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Perl]`_`f.

Some languages (`F33f`_`[Ruby`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ruby_(programming_language)]`_`f, `F33f`_`[Smalltalk`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Smalltalk]`_`f) achieve property-like syntax using normal methods, sometimes with a limited amount of `F33f`_`[syntactic sugar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Syntactic_sugar]`_`f.

>>Syntax variants

Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.

Among these conventions:

• Dot notation
• Bracket notation

>>>Dot notation

The following example demonstrates dot notation in JavaScript.

`B100`F9d9document.createElement('pre');`f`b

>>>Bracket notation

The following example demonstrates bracket notation in JavaScript.

`B100`F9d9document['createElement']('pre');`f`b

>>Example syntax

>>>C#

`B100`F9d9class Pen`f`b
`B100`F9d9{`f`b
`B100`F9d9 private int color; // private field`f`b
`B100`F9d9`f`b
`B100`F9d9 // public property`f`b
`B100`F9d9 public int Color`f`b
`B100`F9d9 {`f`b
`B100`F9d9 get`f`b
`B100`F9d9 {`f`b
`B100`F9d9 return this.color;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 set`f`b
`B100`F9d9 {`f`b
`B100`F9d9 if (value > 0) {`f`b
`B100`F9d9 this.color = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

`B100`F9d9// accessing:`f`b
`B100`F9d9Pen pen = new Pen();`f`b
`B100`F9d9int color_tmp = 0;`f`b
`B100`F9d9// ...`f`b
`B100`F9d9pen.Color = 17;`f`b
`B100`F9d9color_tmp = pen.Color;`f`b
`B100`F9d9// ...`f`b
`B100`F9d9pen.Color = ~pen.Color; // bitwise complement ...`f`b
`B100`F9d9`f`b
`B100`F9d9// another silly example:`f`b
`B100`F9d9pen.Color += 1; // a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!`f`b

Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter. However, it can be private.

`B100`F9d9class Shape`f`b
`B100`F9d9{`f`b
`B100`F9d9 public int Height { get; set; }`f`b
`B100`F9d9 public int Width { get; private set; }`f`b
`B100`F9d9}`f`b

>>>C++

`F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:

>>>>Using Standard C++

`B100`F9d9#include <iostream>`f`b
`B100`F9d9`f`b
`B100`F9d9template <typename T> class property {`f`b
`B100`F9d9 T value;`f`b
`B100`F9d9 public:`f`b
`B100`F9d9 T & operator = (const T &i) {`f`b
`B100`F9d9 return value = i;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 // This template class member function template serves the purpose to make`f`b
`B100`F9d9 // typing more strict. Assignment to this is only possible with exact identical types.`f`b
`B100`F9d9 // The reason why it will cause an error is temporary variable created while implicit type conversion in reference initialization.`f`b
`B100`F9d9 template <typename T2> T2 & operator = (const T2 &i) {`f`b
`B100`F9d9 T2 &guard = value;`f`b
`B100`F9d9 throw guard; // Never reached.`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 // Implicit conversion back to T.`f`b
`B100`F9d9 operator T const & () const {`f`b
`B100`F9d9 return value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9struct Foo {`f`b
`B100`F9d9 // Properties using unnamed classes.`f`b
`B100`F9d9 class {`f`b
`B100`F9d9 int value;`f`b
`B100`F9d9 public:`f`b
`B100`F9d9 int & operator = (const int &i) { return value = i; }`f`b
`B100`F9d9 operator int () const { return value; }`f`b
`B100`F9d9 } alpha;`f`b
`B100`F9d9`f`b
`B100`F9d9 class {`f`b
`B100`F9d9 float value;`f`b
`B100`F9d9 public:`f`b
`B100`F9d9 float & operator = (const float &f) { return value = f; }`f`b
`B100`F9d9 operator float () const { return value; }`f`b
`B100`F9d9 } bravo;`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9struct Bar {`f`b
`B100`F9d9 // Using the property<>-template.`f`b
`B100`F9d9 property <bool> alpha;`f`b
`B100`F9d9 property <unsigned int> bravo;`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9int main () {`f`b
`B100`F9d9 Foo foo;`f`b
`B100`F9d9 foo.alpha = 5;`f`b
`B100`F9d9 foo.bravo = 5.132f;`f`b
`B100`F9d9`f`b
`B100`F9d9 Bar bar;`f`b
`B100`F9d9 bar.alpha = true;`f`b
`B100`F9d9 bar.bravo = true; // This line will yield a compile time error`f`b
`B100`F9d9 // due to the guard template member function.`f`b
`B100`F9d9 ::std::cout << foo.alpha << ", "`f`b
`B100`F9d9 << foo.bravo << ", "`f`b
`B100`F9d9 << bar.alpha << ", "`f`b
`B100`F9d9 << bar.bravo`f`b
`B100`F9d9 << ::std::endl;`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b

Also see Stack Overflow for a more detailed example.

>>>>C++, Microsoft, GCC, LLVM/clang and C++Builder-specific

An example taken from the MSDN documentation page.

`B100`F9d9// declspec_property.cpp`f`b
`B100`F9d9struct S`f`b
`B100`F9d9{`f`b
`B100`F9d9 int i;`f`b
`B100`F9d9 void putprop(int j)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 i = j;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 int getprop()`f`b
`B100`F9d9 {`f`b
`B100`F9d9 return i;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 __declspec(property(get = getprop, put = putprop)) int the_prop;`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9int main()`f`b
`B100`F9d9{`f`b
`B100`F9d9 S s;`f`b
`B100`F9d9 s.the_prop = 5;`f`b
`B100`F9d9 return s.the_prop;`f`b
`B100`F9d9}`f`b

>>>D

`B100`F9d9class Pen`f`b
`B100`F9d9{`f`b
`B100`F9d9 private int m_color; // private field`f`b
`B100`F9d9`f`b
`B100`F9d9 // public get property`f`b
`B100`F9d9 public int color () {`f`b
`B100`F9d9 return m_color;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 // public set property`f`b
`B100`F9d9 public void color (int value) {`f`b
`B100`F9d9 m_color = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

`B100`F9d9auto pen = new Pen;`f`b
`B100`F9d9pen.color = ~pen.color; // bitwise complement`f`b
`B100`F9d9`f`b
`B100`F9d9// the set property can also be used in expressions, just like regular assignment`f`b
`B100`F9d9int theColor = (pen.color = 0xFF0000);`f`b

In D version 2, each property accessor or mutator must be marked with @property:

`B100`F9d9class Pen`f`b
`B100`F9d9{`f`b
`B100`F9d9 private int m_color; // private field`f`b
`B100`F9d9`f`b
`B100`F9d9 // public get property`f`b
`B100`F9d9 @property public int color () {`f`b
`B100`F9d9 return m_color;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 // public set property`f`b
`B100`F9d9 @property public void color (int value) {`f`b
`B100`F9d9 m_color = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

>>>Delphi/Free Pascal

`B100`F9d9type TPen = class`f`b
`B100`F9d9 private`f`b
`B100`F9d9 FColor: TColor;`f`b
`B100`F9d9 function GetColor: TColor;`f`b
`B100`F9d9 procedure SetColor(const AValue: TColor);`f`b
`B100`F9d9 public`f`b
`B100`F9d9 property Color: Integer read GetColor write SetColor;`f`b
`B100`F9d9end;`f`b
`B100`F9d9`f`b
`B100`F9d9function TPen.GetColor: TColor;`f`b
`B100`F9d9begin`f`b
`B100`F9d9 Result := FColor;`f`b
`B100`F9d9end;`f`b
`B100`F9d9`f`b
`B100`F9d9procedure TPen.SetColor(const AValue: TColor);`f`b
`B100`F9d9begin`f`b
`B100`F9d9 if FColor <> AValue`f`b
`B100`F9d9 then FColor := AValue;`f`b
`B100`F9d9end;`f`b

`B100`F9d9// accessing:`f`b
`B100`F9d9var Pen: TPen;`f`b
`B100`F9d9// ...`f`b
`B100`F9d9Pen.Color := not Pen.Color;`f`b
`B100`F9d9`f`b
`B100`F9d9(*`f`b
`B100`F9d9Delphi and Free Pascal also support a 'direct field' syntax -`f`b
`B100`F9d9`f`b
`B100`F9d9property Color: TColor read FColor write SetColor;`f`b
`B100`F9d9`f`b
`B100`F9d9or`f`b
`B100`F9d9`f`b
`B100`F9d9property Color: TColor read GetColor write FColor;`f`b
`B100`F9d9`f`b
`B100`F9d9where the compiler generates the exact same code as for reading and writing`f`b
`B100`F9d9a field. This offers the efficiency of a field, with the safety of a property.`f`b
`B100`F9d9(You can't get a pointer to the property, and you can always replace the member`f`b
`B100`F9d9access with a method call.)`f`b
`B100`F9d9*)`f`b

>>>eC

`B100`F9d9class Pen`f`b
`B100`F9d9{`f`b
`B100`F9d9 // private data member`f`b
`B100`F9d9 Color color;`f`b
`B100`F9d9public:`f`b
`B100`F9d9 // public property`f`b
`B100`F9d9 property Color color`f`b
`B100`F9d9 {`f`b
`B100`F9d9 get { return color; }`f`b
`B100`F9d9 set { color = value; }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
`B100`F9d9Pen blackPen { color = black };`f`b
`B100`F9d9Pen whitePen { color = white };`f`b
`B100`F9d9Pen pen3 { color = { 30, 80, 120 } };`f`b
`B100`F9d9Pen pen4 { color = ColorHSV { 90, 20, 40 } };`f`b

>>>F#

`B100`F9d9type Pen() = class`f`b
`B100`F9d9 let mutable _color = 0`f`b
`B100`F9d9`f`b
`B100`F9d9 member this.Color`f`b
`B100`F9d9 with get() = _color`f`b
`B100`F9d9 and set value = _color <- value`f`b
`B100`F9d9end`f`b

`B100`F9d9let pen = new Pen()`f`b
`B100`F9d9pen.Color <- ~~~pen.Color`f`b

>>>JavaScript

`B100`F9d9function Pen() {`f`b
`B100`F9d9 this._color = 0;`f`b
`B100`F9d9}`f`b
`B100`F9d9// Add the property to the Pen type itself, can also`f`b
`B100`F9d9// be set on the instance individually`f`b
`B100`F9d9Object.defineProperties(Pen.prototype, {`f`b
`B100`F9d9 color: {`f`b
`B100`F9d9 get: function () {`f`b
`B100`F9d9 return this._color;`f`b
`B100`F9d9 },`f`b
`B100`F9d9 set: function (value) {`f`b
`B100`F9d9 this._color = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9});`f`b

`B100`F9d9var pen = new Pen();`f`b
`B100`F9d9pen.color = ~pen.color; // bitwise complement`f`b
`B100`F9d9pen.color += 1; // Add one`f`b

>>>ActionScript 3.0

`B100`F9d9package {`f`b
`B100`F9d9 public class Pen {`f`b
`B100`F9d9 private var _color:uint = 0;`f`b
`B100`F9d9`f`b
`B100`F9d9 public function get color ():uint {`f`b
`B100`F9d9 return _color;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public function set color(value:uint):void {`f`b
`B100`F9d9 _color = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

`B100`F9d9var pen:Pen = new Pen();`f`b
`B100`F9d9pen.color = ~pen.color; // bitwise complement`f`b
`B100`F9d9pen.color += 1; // add one`f`b

>>>Objective-C 2.0

`B100`F9d9@interface Pen : NSObject`f`b
`B100`F9d9@property (copy) NSColor *colour; // The "copy" attribute causes the object's copy to be`f`b
`B100`F9d9 // retained, instead of the original.`f`b
`B100`F9d9@end`f`b
`B100`F9d9`f`b
`B100`F9d9@implementation Pen`f`b
`B100`F9d9@synthesize colour; // Compiler directive to synthesise accessor methods.`f`b
`B100`F9d9 // It can be left behind in Xcode 4.5 and later.`f`b
`B100`F9d9@end`f`b

The above example could be used in an arbitrary method like this:

`B100`F9d9Pen *pen = [[Pen alloc] init];`f`b
`B100`F9d9pen.colour = [NSColor blackColor];`f`b
`B100`F9d9float red = pen.colour.redComponent;`f`b
`B100`F9d9[pen.colour drawSwatchInRect: NSMakeRect(0, 0, 100, 100)];`f`b

>>>PHP

`B100`F9d9class Pen`f`b
`B100`F9d9{`f`b
`B100`F9d9 private int $color = 1;`f`b
`B100`F9d9`f`b
`B100`F9d9 function __set($property, $value)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 if (property_exists($this, $property)) {`f`b
`B100`F9d9 $this->$property = $value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 function __get($property)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 if (property_exists($this, $property)) {`f`b
`B100`F9d9 return $this->$property;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 return null;`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

`B100`F9d9$p = new Pen();`f`b
`B100`F9d9$p->color = ~$p->color; // Bitwise complement`f`b
`B100`F9d9echo $p->color;`f`b

>>>Python

Properties only work correctly for new-style classes (classes that have `B100`F9d9object`f`b as a `F33f`_`[superclass`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Superclass_(computer_science)]`_`f), and are only available in Python 2.2 and newer (see the relevant section of the tutorial `*Unifying types and classes in Python 2.2`*). Python 2.6 added a new syntax involving decorators for defining properties.

`B100`F9d9class Pen:`f`b
`B100`F9d9 def __init__(self) -> None:`f`b
`B100`F9d9 self._color = 0 # "private" variable`f`b
`B100`F9d9`f`b
`B100`F9d9 @property`f`b
`B100`F9d9 def color(self):`f`b
`B100`F9d9 return self._color`f`b
`B100`F9d9`f`b
`B100`F9d9 @color.setter`f`b
`B100`F9d9 def color(self, color):`f`b
`B100`F9d9 self._color = color`f`b

`B100`F9d9pen = Pen()`f`b
`B100`F9d9# Accessing:`f`b
`B100`F9d9pen.color = ~pen.color # Bitwise complement ...`f`b

>>>Ruby

`B100`F9d9class Pen`f`b
`B100`F9d9 def initialize`f`b
`B100`F9d9 @color = 0`f`b
`B100`F9d9 end`f`b
`B100`F9d9`f`b
`B100`F9d9 # Defines a getter for the @color field`f`b
`B100`F9d9 def color`f`b
`B100`F9d9 @color`f`b
`B100`F9d9 end`f`b
`B100`F9d9`f`b
`B100`F9d9 # Defines a setter for the @color field`f`b
`B100`F9d9 def color=(value)`f`b
`B100`F9d9 @color = value`f`b
`B100`F9d9 end`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9pen = Pen.new`f`b
`B100`F9d9pen.color = ~pen.color # Bitwise complement`f`b

Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.

`B100`F9d9class Pen`f`b
`B100`F9d9 attr_reader :brand # Generates a getter for @brand (Read-Only)`f`b
`B100`F9d9 attr_writer :size # Generates a setter for @size (Write-Only)`f`b
`B100`F9d9 attr_accessor :color # Generates both a getter and setter for @color (Read/Write)`f`b
`B100`F9d9`f`b
`B100`F9d9 def initialize`f`b
`B100`F9d9 @color = 0 # Within the object, we can access the instance variable directly`f`b
`B100`F9d9 @brand = "Penbrand"`f`b
`B100`F9d9 @size = 0.7 # But we could also use the setter method defined by the attr_accessor Class instance method`f`b
`B100`F9d9 end`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9pen = Pen.new`f`b
`B100`F9d9puts pen.brand # Accesses the pen brand through the generated getter`f`b
`B100`F9d9pen.size = 0.5 # Updates the size field of the pen through the generated setter`f`b
`B100`F9d9pen.color = ~pen.color`f`b

>>>Visual Basic

>>>>Visual Basic (.NET 2003–2010)

`B100`F9d9Public Class Pen`f`b
`B100`F9d9`f`b
`B100`F9d9 Private _color As Integer ' Private field`f`b
`B100`F9d9`f`b
`B100`F9d9 Public Property Color() As Integer ' Public property`f`b
`B100`F9d9 Get`f`b
`B100`F9d9 Return _color`f`b
`B100`F9d9 End Get`f`b
`B100`F9d9 Set(ByVal value As Integer)`f`b
`B100`F9d9 _color = value`f`b
`B100`F9d9 End Set`f`b
`B100`F9d9 End Property`f`b
`B100`F9d9`f`b
`B100`F9d9End Class`f`b

`B100`F9d9' Create Pen class instance`f`b
`B100`F9d9Dim pen As New Pen()`f`b
`B100`F9d9`f`b
`B100`F9d9' Set value`f`b
`B100`F9d9pen.Color = 1`f`b
`B100`F9d9`f`b
`B100`F9d9' Get value`f`b
`B100`F9d9Dim color As Int32 = pen.Color`f`b

>>>>Visual Basic (only .NET 2010)

`B100`F9d9Public Class Pen`f`b
`B100`F9d9`f`b
`B100`F9d9 Public Property Color() As Integer ' Public property`f`b
`B100`F9d9`f`b
`B100`F9d9End Class`f`b

`B100`F9d9' Create Pen class instance`f`b
`B100`F9d9Dim pen As New Pen()`f`b
`B100`F9d9`f`b
`B100`F9d9' Set value`f`b
`B100`F9d9pen.Color = 1`f`b
`B100`F9d9`f`b
`B100`F9d9' Get value`f`b
`B100`F9d9Dim color As Int32 = pen.Color`f`b

>>>>Visual Basic 6

`B100`F9d9' in a class named clsPen`f`b
`B100`F9d9Private m_Color As Long`f`b
`B100`F9d9`f`b
`B100`F9d9Public Property Get Color() As Long`f`b
`B100`F9d9 Color = m_Color`f`b
`B100`F9d9End Property`f`b
`B100`F9d9`f`b
`B100`F9d9Public Property Let Color(ByVal RHS As Long)`f`b
`B100`F9d9 m_Color = RHS`f`b
`B100`F9d9End Property`f`b

`B100`F9d9' accessing:`f`b
`B100`F9d9Dim pen As New clsPen`f`b
`B100`F9d9' ...`f`b
`B100`F9d9pen.Color = Not pen.Color`f`b

>>See also

• `F33f`_`[Attribute (computing)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Attribute_(computing)]`_`f
• `F33f`_`[Bound property`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bound_property]`_`f
• `F33f`_`[Field (computer science)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Field_(computer_science)]`_`f
• `F33f`_`[Indexer (programming)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Indexer_(programming)]`_`f
• `F33f`_`[Method (computer programming)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Method_(computer_programming)]`_`f
• `F33f`_`[Mutator method`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutator_method]`_`f
• `F33f`_`[Uniform access principle`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Uniform_access_principle]`_`f

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f "Accessors And Mutators In Java". `*C# Corner - Community of Software and Data Developers`*. Retrieved 5 January 2022.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Portability of Native C++ properties". `*Stack Overflow`*. Stack Overflow. Retrieved 5 January 2022.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f `:21-november-2021`a"property (C++)". `*Microsoft technical documentation`*. Microsoft. Retrieved 5 January 2022.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f "clang::MSPropertyDecl Class Reference". `*Clang: a C language family frontend for LLVM`*. Retrieved 5 January 2022.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "__property Keyword Extension". `*Embarcadero/IDERA Documentation Wiki`*. Retrieved 5 January 2022.

`c`F0af`_`[↑ Back to top`#top]`_`f`a